from game import zz_game
from zz import zz_log, zz_img
import time
from game import game_几何王国, game_淘宝农场_钩鱼


class Game(zz_game.ZZ_Game):
    def __init__(self, d):
        super().__init__(d)

    def get_name(self):
        return "淘宝农场_砸蛋"

    def play_egg(self):
        self.log_msg("开始砸蛋")
        self.d.sleep(2)
        if self.img_exit('./game/img/淘宝农场_砸蛋/play_alert_close.png', 2):
            self.log_msg("存在弹窗，点击关闭")
            self.img_click('./game/img/淘宝农场_砸蛋/play_alert_close.png')

        egg_left_count = 0
        try:
            child = self.d.xpath('//*[@resource-id="smash-egg"]').child('//android.widget.TextView').all()
            egg_left_count = int(child[0].text)
        except Exception as ex:
            pass
        self.log_msg(f"剩余砸蛋次数：{egg_left_count}")
        if (egg_left_count > 0):
            # for i in range(egg_left_count):
            for i in range(2):
                self.log_msg(f"{i}-点击砸蛋")
                self.d.xpath('//*[@resource-id="smash-egg"]').click()
                self.img_exit('./game/img/淘宝农场_砸蛋/play_alert_main.png', 10)
                self.log_msg(f"{i}-点击砸蛋-完成")
                self.log_msg(f"{i}-点击关闭")
                self.img_click('./game/img/淘宝农场_砸蛋/play_alert_close.png')
        self.log_msg(f"点击关闭任务列表")
        self.img_click('./game/img/淘宝农场_砸蛋/task_close.png')

    def handle_main(self):
        if zz_game.config_get('__task_baba') == 1:
            self.log_msg("砸蛋和钓鱼任务已完成")
            return True
        self.log_msg("点击芭芭农场")
        # self.d.xpath('//*[@text="芭芭农场"]').click()
        self.img_click("./game/img/淘宝农场_砸蛋/game_index.png")
        if not self.img_exit("./game/img/淘宝农场_砸蛋/play_index.png", 20):
            self.log_msg("芭芭农场等待超时, 请重新运行")
            return False

        if zz_game.config_get('__task_baba') is None:
            self.log_msg("已进入芭芭农场，点击农场乐园")
            if self.d.xpath('//*[@text="农场乐园入口icon"]').wait(2):
                self.d.xpath('//*[@text="农场乐园入口icon"]').click()
            else:
                self.log_msg("未找到入口，尝试用图片找入口")
                self.img_click("./game/img/淘宝农场_砸蛋/play_index.png")
            self.d.sleep(2)
            childs = self.d.xpath('//*[@resource-id="game-center-content"]').child('//android.widget.TextView').all()[0:10]
            count = 0
            count_max = 20
            count_get = False
            for item in childs:
                txt = item.text
                if count_get:
                    count = int(txt)
                    break
                if txt == "已得：":
                    count_get = True
            self.log_msg(f"农场乐园：{count}/{count_max}")
            if count >= count_max:
                self.log_msg("农场乐园任务已完成")
            else:
                self.log_msg("开始搜索几何王国")
                go_img_arr = [
                    './game/img/淘宝农场_砸蛋/baba_game_main.png',
                    './game/img/淘宝农场_砸蛋/baba_game_main_01.png',
                    './game/img/淘宝农场_砸蛋/baba_game_main_02.png',
                ]
                go_img = None
                while True:
                    find_ok = False
                    for img in go_img_arr:
                        if self.img_exit(img, 2):
                            go_img = img
                            find_ok = True
                            break
                    if find_ok:
                        break
                    self.swipe_up()
                self.log_msg("点击进入几何王国")
                self.img_click(go_img)
                game = game_几何王国.Game(self.d, (count_max - count) * 3)
                game.handle_main()
                self.close_game()

            zz_game.config_set('__task_baba', 2)  # 标记完成
            account_id = zz_game.config_get('__account_id')
            if account_id == 'x' or account_id == 'xx':
                self.log_msg("指定帐号不砸蛋")
                self.d.sleep(2)
                if self.img_exit('./game/img/淘宝农场_砸蛋/play_alert_close.png', 2):
                    self.log_msg("存在弹窗，点击关闭")
                    self.img_click('./game/img/淘宝农场_砸蛋/play_alert_close.png')
                self.log_msg(f"点击关闭任务列表")
                self.img_click('./game/img/淘宝农场_砸蛋/task_close.png')
            else:
                self.play_egg()
                self.play_egg()

            if self.img_exit("./game/img/淘宝农场_砸蛋/task_close.png", 2):
                self.log_msg(f"点击关闭任务列表")
                self.img_click('./game/img/淘宝农场_砸蛋/task_close.png')

            self.log_msg("点击任务列表")
            if self.d.xpath('//android.widget.Button[@text="任务列表"]').wait(5):
                self.d.xpath('//android.widget.Button[@text="任务列表"]').click()
                self.img_exit("./game/img/淘宝农场_砸蛋/task_list.png")
                self.log_msg("开始领任务奖励")
                try_index = 0
                while True:
                    if not self.img_exit("./game/img/淘宝农场_砸蛋/task_get.png", 2, 0.9):
                        if try_index > 5:
                            self.log_msg("已完成领取奖励")
                            break
                        self.swipe_up()
                        try_index += 1
                    self.img_click("./game/img/淘宝农场_砸蛋/task_get.png", 2, 1, 0.9)
                self.d.sleep(2)
                self.log_msg("点击关闭任务列表")
                self.img_click("./game/img/淘宝农场_砸蛋/play_alert_close.png", 3)
        else:
            self.log_msg("砸蛋任务已完成")

        # fish_pic = "./game/img/淘宝农场_钩鱼/game_index_01.png"
        # fish = self.img_exit("./game/img/淘宝农场_钩鱼/game_index_01.png", 2)
        # if not fish:
        #     fish_pic = "./game/img/淘宝农场_钩鱼/game_index.png"
        #     fish = self.img_exit("./game/img/淘宝农场_钩鱼/game_index.png", 2)
        # self.log_msg(f"钩鱼是否存在：{fish}")
        # if fish:
        #     self.log_msg(f"点击进入钩鱼活动")
        #     self.img_click(fish_pic)
        #     game_淘宝农场_钩鱼.Game(self.d).handle_main()

        zz_game.config_set('__task_baba', 1)  # 标记完成
        self.d.sleep(2)
        self.close_game()

        return True
